package com.tedu.charging.service.device.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


import com.tedu.charging.service.device.constant.RedisConstants;
import com.tedu.charging.service.device.dao.ChargingStationMapper;

import com.tedu.charging.service.device.pojo.po.ChargingStationPO;
import com.tedu.charging.service.device.pojo.vo.ChargingStationVO;
import com.tedu.charging.service.device.pojo.vo.NearbyChargingStationVO;
import com.tedu.charging.service.device.service.IChargingStationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.BoundGeoOperations;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


/**
 * 充电站Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-10
 */
@Service
public class ChargingStationServiceImpl implements IChargingStationService
{
    @Autowired
    private ChargingStationMapper chargingStationMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public List<NearbyChargingStationVO> selectNearbyStation(double longitude, double latitude, double radius) {
        BoundGeoOperations boundGeoOperations = redisTemplate.boundGeoOps(RedisConstants.STATION_GEO_ZSET);
        //1,创建用户的位置对象 point
        Point userPosition = new Point(longitude, latitude);
        //2,创建查找范围距离distance
        Distance distance = new Distance(radius, Metrics.KILOMETERS);
        //3,创建circle
        Circle circle = new Circle(userPosition, distance);
        //4,设置参数
        RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs();
        //4.1 设置redis返回距离
        args.includeDistance();
        //4.2 设置redis返回坐标
        args.includeCoordinates();
        //5,调用redis查找附近充电站
        GeoResults<RedisGeoCommands.GeoLocation<String>> geoResults = boundGeoOperations.radius(circle, args);

        //6,创建操作stationDetailHash的对象
        BoundHashOperations boundHashOperations = redisTemplate.boundHashOps(RedisConstants.STATION_DETAIL_HASH);
        ArrayList<NearbyChargingStationVO> arrayList = new ArrayList<>();

        //7,得到迭代器
        Iterator<GeoResult<RedisGeoCommands.GeoLocation<String>>> iterator = geoResults.iterator();
        //8,遍历迭代器
        while (iterator.hasNext()) {
            GeoResult<RedisGeoCommands.GeoLocation<String>> geoResult = iterator.next();
            //8.1 得到id
            String id = geoResult.getContent().getName();
            //8.2 根据id从stationDetailHash得到充电站详情

            ChargingStationVO chargingStationVO = (ChargingStationVO) boundHashOperations.get(id);
            //8.3 得到距离
            double stationDistance = geoResult.getDistance().getValue();
            //8.4 创建vo
            NearbyChargingStationVO nearbyChargingStationVO = new NearbyChargingStationVO();
            BeanUtils.copyProperties(chargingStationVO,nearbyChargingStationVO);
            nearbyChargingStationVO.setDistance(stationDistance);
            //8.5 把vo放到集合
            arrayList.add(nearbyChargingStationVO);
        }

        return arrayList;
    }

    /**
     * 查询充电站
     *
     * @param id 充电站主键
     * @return 充电站
     */
    @Override
    public ChargingStationVO selectChargingStationById(Integer id)
    {
        //从redis中取数据
        BoundHashOperations boundHashOperations = redisTemplate.boundHashOps(RedisConstants.STATION_DETAIL_HASH);
        ChargingStationVO chargingStationVO = (ChargingStationVO) boundHashOperations.get(id.toString());

        return chargingStationVO;

    }

    /**
     * 查询充电站列表
     * 
     *
     * @return 充电站
     */
    @Override
    public List<ChargingStationPO> selectChargingStationList()
    {
        return chargingStationMapper.selectList(null);
    }


}
